const formatNumber = n => {
	n = n.toString()
	return n[1] ? n : '0' + n
}

/************时间日期处理************************/
function formatDateTime(date, connector) {
	const year = date.getFullYear()
	const month = date.getMonth() + 1
	const day = date.getDate()
	const hour = date.getHours()
	const minute = date.getMinutes()
	const second = date.getSeconds()
	const millisecond = date.getMilliseconds()

	if (connector === '') {
		return [year, month, day].map(formatNumber).join('') + [hour, minute, second].map(formatNumber).join('') + [
			millisecond
		].map(formatNumber)
	} else {
		return [year, month, day].map(formatNumber).join('-') +
			' ' + [hour, minute, second].map(formatNumber).join(':') +
			'.' + [millisecond].map(formatNumber)
	}
}
// 是否为空
function isNull(obj) {
	let result = false
	if (obj == null) result = true
	else if (obj == undefined) result = true
	else if (obj == 'null') result = true
	else if (obj == 'undefined') result = true
	else if (obj == '') result = true
	else if (obj == '[]') result = true
	else if (obj == '{}') result = true
	else if (JSON.stringify(obj) == '[]') result = true
	else if (JSON.stringify(obj) == '{}') result = true
	else if (obj == []) result = true
	else if (obj == {}) result = true
	else if (obj.length <= 0) result = true
	return result
}
/**************
 * 其他
 */
function getErpToken() {
	const num = [1, 3, 1, 5, 7, 1, 3, 5, 1, 3, 7, 9]
	const date = new Date()
	const year = date.getFullYear()
	const month = date.getMonth() + 1
	const day = date.getDate()
	const hour = date.getHours()
	const minute = date.getMinutes()
	const second = date.getSeconds()
	// const millisecond = date.getMilliseconds()
	let s = [minute, day, month, year.toString().substring(2, 4), second, hour].map(formatNumber).join('')
	// nnddmmyysshh 分日月年秒时
	// console.log(s)
	let r = 0
	for (let i = 0; i < 12; i++) {
		let t = s.substring(i, i * 1 + 1) || '0'
		r = r * 1 + t * num[i]
	}
	r = r * 1 + 100
	let w = r.toString()
	let wl = w.length
	w = w.substring(wl - 2, wl)
	s = s + w + 'erp'
	return s
}
// android 导出excel
function appExportFile(jsonData, str) {
	// jsonData = [{name: '测试数据',phone: '123456',email: '123@456.com'}]
	// str = '<tr><td>序号</td><td>条码</td><td>备注</td></tr>'
	let worksheet = 'sheet1'
	// 循环遍历，每行加入tr标签，每个单元格加td标签
	for (let i = 0; i < jsonData.length; i++) {
		str += '<tr>'
		for (let item in jsonData[i]) {
			// 增加\t为了不让表格显示科学计数法或者其他格式
			str += `<td>${jsonData[i][item] + '\t'}</td>`
		}
		str += '</tr>'
	}
	// 下载的表格模板数据
	let fileData = `<html xmlns:o="urn:schemas-microsoft-com:office:office" 
	      xmlns:x="urn:schemas-microsoft-com:office:excel" 
	      xmlns="http://www.w3.org/TR/REC-html40">
	      <head><!--[if gte mso 9]><xml encoding="UTF-8"><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>
	      <x:Name>${worksheet}</x:Name>
	      <x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>
	      </x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->
	      </head><body><table>${str}</table></body></html>`

	plus.io.requestFileSystem(plus.io.PUBLIC_DOCUMENTS, function(fs) {
		let rootObj = fs.root
		let rootPath = rootObj.fullPath
		rootObj.getDirectory('excel', {
			create: true
		}, function(dirEntry) {
			let fileName = 'scan' + formatDateTime(new Date(), '')
			dirEntry.getFile(`${fileName}.xlsx`, {
				create: true
			}, function(fileEntry) {
				fileEntry.createWriter(function(writer) {
					writer.onwritestart = (e) => {
						uni.showToast({
							title: '生成Excel文件…',
							icon: 'loading',
							mask: true
						})
					}
					writer.write(fileData)
					writer.onwrite = (e) => {
						let fileUrl = e.target.fileName
						uni.openDocument({
							filePath: fileUrl
						})
					}
				}, function(e) {
					console.log(e.message)
					uni.showToast({
						title: e.message,
						duration: 2000,
						icon: 'none'
					})
				})
			})
		})
	})
}

export default {
	formatDateTime,
	isNull,
	getErpToken,
	appExportFile
}
